<?php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {
	protected function _initDatabase() {
		$config = $this -> getOptions();
		$db1 = Zend_Db::factory($config['resources']['multidb']['mydb1']['adapter'], $config['resources']['multidb']['mydb1']);
		Zend_Db_Table::setDefaultAdapter($db1);
		$db1 -> setFetchMode(Zend_Db::FETCH_OBJ);
		Zend_Registry::set("global", $db1);

		$db2 = Zend_Db::factory($config['resources']['multidb']['mydb2']['adapter'], $config['resources']['multidb']['mydb2']);
		$db2 -> setFetchMode(Zend_Db::FETCH_OBJ);
		Zend_Registry::set("merchant", $db2);
        
        $db3 = Zend_Db::factory($config['resources']['multidb']['mydb3']['adapter'], $config['resources']['multidb']['mydb3']);
        $db3 -> setFetchMode(Zend_Db::FETCH_OBJ);
        Zend_Registry::set("shard1", $db3);
        
        $db4 = Zend_Db::factory($config['resources']['multidb']['mydb4']['adapter'], $config['resources']['multidb']['mydb4']);
        $db4 -> setFetchMode(Zend_Db::FETCH_OBJ);
        Zend_Registry::set("shard2", $db4);

	}
    

	protected function _initConstant() {
		$registry = Zend_Registry::getInstance();
		$registry -> constants = new Zend_Config($this -> getApplication() -> getOption('constants'));
	}

	protected function _initLog() {
		$options = $this -> getOption('resources');

		$partitionConfig = $this -> getOption('log');
		$logOptions = $options['log'];

		$baseFilename = $logOptions['stream']['writerParams']['stream'];
		if ($partitionConfig['partitionStrategy'] == 'context') {
			$baseFilename = $partitionConfig['path'] . '/' . APPLICATION_ENV;
		}

		$logFilename = '';
		switch(strtolower($partitionConfig['partitionFrequency'])) {
			case 'daily' :
				$logFilename = $baseFilename . '_' . date('Y_m_d').".log";
				break;

			case 'weekly' :
				$logFilename = $baseFilename . '_' . date('Y_W').".log";
				break;

			case 'monthly' :
				$logFilename = $baseFilename . '_' . date('Y_m').".log";
				break;

			case 'yearly' :
				$logFilename = $baseFilename . '_' . date('Y').".log";
				break;

			default :
				$logFilename = $baseFilename.".log";
		}

		$logOptions['stream']['writerParams']['stream'] = $logFilename;

		$logger = Zend_Log::factory($logOptions);
		Zend_Registry::set('logger', $logger);

		return $logger;
	}

}
